﻿using FastReport;
using FastReport.Utils;
using SMALLERP;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XJLSC.QM
{
    public partial class GCZRPD : Form
    {
        DDKC f;
        private readonly DataBase db = new DataBase();
        private Report FReport;
        private DataSet FDataSet;
        private DataSet FDataSet1;
        private string FReportsFolder;
        private bool FReportRunning;
        public GCZRPD()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String strSql = "";
            if (radioButton2.Checked)
            {
                strSql = "SELECT TH003 日期,RTRIM(TI004) 品号,RTRIM(TI005) 品名,TI006 规格,CAST(TI007 AS REAL) 入库数量,CAST(TI021 AS REAL) 报废数量,TI040 备注,TI001 单别,TI002 单号,TI003 序号 FROM MOCTI LEFT JOIN MOCTH ON TI001=TH001 AND TI002=TH002 WHERE TI021>0 AND TH003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";

            }
            else if(radioButton1.Checked)
            {
                strSql = "SELECT TF003 日期,RTRIM(TG004) 品号,RTRIM(TG005) 品名,TG006 规格,CAST(TG011 AS REAL) 入库数量,CAST(TG012 AS REAL) 报废数量,TG020 备注,TG001 单别,TG002 单号,TG003 序号 FROM MOCTG LEFT JOIN MOCTF ON TG001=TF001 AND TG002=TF002 WHERE TG012>0 AND TF003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";
            }
            else if(radioButton3.Checked)
            {
                strSql = "SELECT TD003 日期,RTRIM(TE017) 品号,RTRIM(TE018) 品名,TE019 规格,CAST(TE011 AS REAL) 入库数量,CAST(SFCTE.UDF51+SFCTE.UDF52 AS REAL) 报废数量,TE015 备注,TE001 单别,TE002 单号,TE003 序号 FROM SFCTE LEFT JOIN SFCTD ON TE001=TD001 AND TE002=TD002 WHERE (SFCTE.UDF51+SFCTE.UDF52)>0 AND TD003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";
            }
            else if (radioButton4.Checked)
            {
                strSql = "SELECT TL003 日期,RTRIM(TM004) 品号,RTRIM(TM005) 品名,TM006 规格,CAST(TM007 AS REAL) 入库数量,CAST(TM007 AS REAL) 报废数量,TM020 备注,TM001 单别,TM002 单号,TM003 序号 FROM INVTM LEFT JOIN INVTL ON TM001=TL001 AND TM002=TL002 WHERE TM007>0 AND TL003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";
            }
            else
            {
                //strSql = "SELECT CC003 日期,CD004 品号,CD005 品名,CD006 规格,CAST(CD008 AS REAL) 入库数量,CAST(TA008 AS REAL) 报废数量,CD035 备注,CD001 单别,CD002 单号,CD003 序号,MA002 供应商 from QMSTA LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN PURCC ON CD001=CC001 AND CD002=CC002 LEFT JOIN PURTD ON CD010=TD001 AND CD011=TD002 AND CD012=TD003 LEFT JOIN PURMA ON CC005=MA001 WHERE TA008>0";// AND CC003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";
                strSql = "SELECT TG003 日期,TH004 品号,TH005 品名,TH006 规格,CAST(TH007 AS REAL) 入库数量,CAST(TH017 AS REAL) 报废数量,TH033 备注,TH001 单别,TH002 单号,TH003 序号,MA002 供应商 from PURTH LEFT JOIN PURTG ON TG001=TH001 AND TG002=TH002 LEFT JOIN PURTD ON TH011=TD001 AND TH012=TD002 AND TH013=TD003 LEFT JOIN PURMA ON TG005=MA001 WHERE TH017>0 AND TG003=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)";
            }
            try
            {
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
                //this.dataGridView4.Columns["图片"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.dataGridView2.Controls.Clear();//移除所有控件 
                if (e.ColumnIndex.Equals(this.dataGridView2.Columns["JTGYS"].Index) && dataGridView2.Rows[e.RowIndex].Cells["ZRBM"].Value.ToString()=="外协")
                //if (e.ColumnIndex == 13)
                //判断单元格是否是"Company"列？    
                {
                    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                    btn.Text = "...";//设置button文字    
                    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                    btn.Visible = true;//设置控件允许显示    

                    btn.Width = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                    btn.Height = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                    btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

                    this.dataGridView2.Controls.Add(btn);//dataGridView1中添加控件btn    

                    btn.Location = new System.Drawing.Point(((this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                            (btn.Width)), this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
                }
            }
            catch
            {

            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            f = new DDKC();
            f.kclx = "gongyingshang";
            f.DataSelected += new EventHandler(f_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }

        void f_DataSelected(object sender, EventArgs e)
        {
            this.dataGridView2.Rows[this.dataGridView2.CurrentCell.RowIndex].Cells[this.dataGridView2.Columns["JTGYS"].Index].Value = f.UserName;

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String strSql = "";
            //以下插入不良及报废数据
            String rkdb = (dataGridView1.CurrentRow.Cells["单别"].Value == null) ? "" : dataGridView1.CurrentRow.Cells["单别"].Value.ToString();
            String rkdh = (dataGridView1.CurrentRow.Cells["单号"].Value == null) ? "" : dataGridView1.CurrentRow.Cells["单号"].Value.ToString();
            String rkxh = (dataGridView1.CurrentRow.Cells["序号"].Value == null) ? "" : dataGridView1.CurrentRow.Cells["序号"].Value.ToString();

            strSql = "DELETE FROM QMSTL WHERE TL001='" + rkdb + "' AND TL002='" + rkdh + "' AND TL003='" + rkxh + "'";

            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }

            for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++)
            {
                    double dqs = (dataGridView2.Rows[i].Cells["BFSL"].Value == null) ? 0.00 : Convert.ToDouble(dataGridView2.Rows[i].Cells["BFSL"].Value);
                    if (dqs > 0.00)
                    {
                    String bfyy = (dataGridView2.Rows[i].Cells["BFYY"].Value == null) ? "" : dataGridView2.Rows[i].Cells["BFYY"].Value.ToString();
                    String jtgys = (dataGridView2.Rows[i].Cells["JTGYS"].Value == null) ? "" : dataGridView2.Rows[i].Cells["JTGYS"].Value.ToString();
                    String bfbz = (dataGridView2.Rows[i].Cells["BZ"].Value == null) ? "" : dataGridView2.Rows[i].Cells["BZ"].Value.ToString();
                    String zrbm = (dataGridView2.Rows[i].Cells["ZRBM"].Value == null) ? "" : dataGridView2.Rows[i].Cells["ZRBM"].Value.ToString();
                    String clyj = (dataGridView2.Rows[i].Cells["CLYJ"].Value == null) ? "" : dataGridView2.Rows[i].Cells["CLYJ"].Value.ToString();
                    strSql = "INSERT INTO QMSTL(CREATOR,FLAG,TL001,TL002,TL003,TL004,TL005,TL006,TL007,TL008,TL009,TL010,TL011,TL012,TL013,TL014,UDF01,UDF02,UDF03,UDF04)  SELECT '" + PropertyClass.OperatorName + "',1,'" + rkdb + "','" + rkdh + "','" + rkxh + "','" + i + "',''," + dqs + ",'" + bfyy + "','0001','',CONVERT(CHAR(8),GETDATE(),112),'',0,0,0,'" + jtgys + "','" + zrbm + "','" + clyj + "','" + bfbz + "'";

                    try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {

                            }
                            else
                            {
                                MessageBox.Show("生成不良数据写入失败，请检查", "软件提示");
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                    }               
            }
            MessageBox.Show("判定成功！","系统提示");
            dataGridView2.Rows.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DesignReport();
        }

        private void DesignReport()
        {
            if (FReportRunning)
                return;
            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\");
            string ReportName = theFolder + "不合格处理单.frx";
            FReport = new Report();
            FReport.Load(ReportName);
            RegisterData();
            FReport.Design();
            //PreviewReport();
        }

        private void RegisterData()
        {
            //FReport.RegisterData(FDataSet, "NorthWind");
            //FReport.RegisterData(FBusinessObject, "Categories BusinessObject");
        }

        private void PreviewReport()
        {
            if (FReportRunning)
                return;

            FReportRunning = true;
            try
            {
                RegisterData();
                FReport.Show();
            }
            finally
            {
                FReportRunning = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String x1 = dataGridView1.CurrentRow.Cells["单别"].Value.ToString(); 
            String x2 = dataGridView1.CurrentRow.Cells["单号"].Value.ToString(); ;
            String x3 = "";
            String x4 = "";
            if (radioButton5.Checked)
            {
                   x4 = "采购进货";
            }
            else if (radioButton1.Checked)
             {
                    x4 = "";
           }

                    String x5 = "";
            String x6 = "";
            String x7 = "";
            String x8 = "";
            JYCX xx = new JYCX(x1, x2, x3, x4, x5, x6, x7, x8);
            BHGCLDPrint g = new BHGCLDPrint();
            g.cdssh = new BHGCLDPrint.Cdssh(textBox1_TextChanged);
            g.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void GCZRPD_Load(object sender, EventArgs e)
        {
            dateTimePicker2.Value = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
            dateTimePicker3.Value = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT A.UDF02 责任部门,ISNULL(MA002,'') 供应商,TL007 报废原因,CAST(SUM(TL006) AS REAL) 数量 FROM (SELECT UDF02,UDF01,TL007,TL006,TL010 FROM QMSTL UNION ALL SELECT INVTL.UDF01,INVTL.UDF04,TL008,TM007,TL003 FROM INVTL LEFT JOIN INVTM ON TL001=TM001 AND TL002=TM002) A LEFT JOIN PURMA ON A.UDF01=MA001  WHERE TL010>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND TL010<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) GROUP BY A.UDF02,ISNULL(MA002,''),TL007";      
            try
            {
                dataGridView3.DataSource = null;
                dataGridView3.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
                //this.dataGridView4.Columns["图片"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView3.ColumnCount; i++)
                    {
                        if (dataGridView3.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView3.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView3.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView3.Columns.Count; k++)
                        {
                            if (dataGridView3.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView3.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView3.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
    }
}
